<?php
/**
 * Created by WarCraft3
 * User: WarCraft3
 * Date: 2021/8/20
 * Time: 10:19
 */

namespace app\api\controller;


use app\common\controller\Api;
use app\common\controller\Redis;
use think\Db;

class Study extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    private $_uid;

    protected function _initialize()
    {
        parent::_initialize();
        $jwt = $this->request->header('Authorization');
        if ($jwt) {
            $this->check_token($jwt);
            $this->_uid = $this->_token['uid'];

        } else {
            $this->success('缺少token', '', '401');
        }
    }

    /*
     * 获取学习地图 每个阶段对应的视频
     * */
    public function pvideos()
    {
        $perid = $this->request->param('perid');
        $typeid = $this->request->param('type')??0;
        $cate_id = Db::name('auser')->where('id',$this->_uid)->value('jobs_id');

        if (!$perid)$this->error('缺少参数perid');
        $arr = $this->getVideo($cate_id,$perid,$typeid);
        //根据排序 未观看的在最上面 0=观看中, 1=已看完 2=未观看
        $cmf_arr = array_column($arr, 'ptype');
        array_multisort($cmf_arr, SORT_DESC, $arr);
        $this->success('成功',$arr);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 学习地图,视频,显示全部信息
     */
    public function allvideos(){
        $page = $this->request->param('page');
        $num = $this->request->param('num');
        if(!$page){
            $page= 1;
        }
        if(!$num){
            $num= 10;
        }
        $redis = Redis::getInstance()->getRedisConn();
        $avideo_ids=cate_avideo($this->_uid);
        $vidnots=is_del_avideo($this->_uid);
        $re = $redis->hGetAll('user_'.$this->_uid);
        $vmodel = new \app\admin\model\Avideo();
        $arr = $vmodel::with('com,category')
            ->whereNotIn('avideo.id',$vidnots)
            ->whereIn('avideo.id',$avideo_ids)
//            ->page($page, $num)
            ->select()->each(function ($item) use($re) {
                if (sizeof($item['com']) > 0) {
                    $item['comments'] = sizeof($item['com']);
                    //查询评论人信息
                    foreach ($item['com'] as $k => $v) {
                        $user = \app\admin\model\Auser::get($v['auser_id']);
                        $item['com'][$k]['name'] = $user->name;
                        $item['com'][$k]['avatar'] = $user->avatar;
                    }
                }else{
                    $item['comments'] = 0;
                }
                $item['ptype'] = '0'; // 0=观看中, 1=已看完 2=未观看
                //从redis中查询 观看与否的标签, 并匹配给每一条记录
                foreach ($re as $kk=>$vv){
                    if ($kk == $item['id']){
                        $item['ptype'] = $vv;
                    }
                }
                return $item;
            })->toArray();
        //根据排序 未观看的在最上面 0=观看中, 1=已看完 2=未观看
        $cmf_arr = array_column($arr, 'ptype');
        array_multisort($cmf_arr, SORT_DESC, $arr);
        $this->success('成功',$arr);
    }
    /*
     * 获取视频信息---统一封装
     * @param $perid 视频所属阶段id
     * */
    private function getVideo($cate_id,$perid,$type = '',$page= 1 ,$num= 10)
    {
        $redis = Redis::getInstance()->getRedisConn();
        $where_type = '';
        if ($type){
            $where_type = ['avideo.type'=>$type];
        }
        $vidnots=is_del_avideo($this->_uid);
        $re = $redis->hGetAll('user_'.$this->_uid);
        $vmodel = new \app\admin\model\Avideo();
        $data = $vmodel::with([
            'com' => function($query) {
                $query->order('id','desc') ;
            }
        ])->with('category')
            ->where('period_id',$perid)
            ->whereNotIn('avideo.id',$vidnots)
            ->where($where_type)
            ->select()->each(function ($item) use($re) {
                if (sizeof($item['com']) > 0) {
                    //查询评论人信息
                    foreach ($item['com'] as $k => $v) {
                        $user = \app\admin\model\Auser::get($v['auser_id']);
                        if(empty($user)){
                            unset($item['com'][$k]);
                        }else{
                            $item['com'][$k]['name'] = $user['name'];
                            $item['com'][$k]['avatar'] = $user['avatar'];
                        }

                    }
                    $coms=$item['com'];
                    $commms=[];
                    foreach ($item['com'] as $k => $v){
                        if($item['com'][$k]['pid']>0){
                            unset($item['com'][$k]);
                        }else{
                            $arr=[];
                            foreach ($coms as $ve) {
                                if ($ve['pid'] == $item['com'][$k]['id']) {
                                    $arr[] = $ve;
                                };
                            };
                            $item['com'][$k]['children']=$arr;
                            $commms[]=$item['com'][$k];
                        }
                    }
                    $item['com']=$commms;
                    $item['comments'] = sizeof($commms);
                }else{
                    $item['comments'] = 0;
                }
                $item['ptype'] = '2'; // 0=观看中, 1=已看完 2=未观看
                //从redis中查询 观看与否的标签, 并匹配给每一条记录
                foreach ($re as $kk=>$vv){
                    if ($kk == $item['id']){
                        $item['ptype'] = $vv;
                    }
                }
                return $item;
            })->toArray();
        if ($cate_id){
            $cateids=pid_cate($cate_id);
            foreach ($data as $k=>$v){
                $cates=explode(',',$data[$k]['cate_id']);
                if(empty(array_intersect($cateids,$cates))){
                    unset($data[$k]);
                }
            }
        }
        $data=page_array($num,$page,$data);
        return $data;
    }



}